﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CRMFramework;

namespace CRMBiz
{
    public static class DefaultCompanyHandler
    {


        /// <summary>
        /// function to get default company data
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns>CompanyList</returns>
        public static DataSet GetDefaultSetting(int pageIndex, int pageSize)
        {
            DataSet ds;
            try
            {
                var sbSql = new StringBuilder("SELECT ua.text AS 'UserName',ub.text AS 'DefaultCompanyName',dc.UsercompanyID as UsercompanyID,dc.UserID as UserID,dc.UserDefaultgroupID as UserDefaultgroupID,(select count(*) from crm_userdefaultcompany) as Total FROM crm_userdefaultcompany dc INNER JOIN umbraconode ua ON ua.id = dc.UserID INNER JOIN umbraconode ub ON ub.id = dc.UserDefaultgroupID WHERE ua.nodeObjectType = '39eb0f98-b348-42a1-8662-e7eb18487560' AND ub.nodeObjectType = '366e63b9-880f-4e13-a61c-98069b029728' Order by ua.text asc");
                sbSql.Append(" LIMIT ").Append((pageIndex - 1) * pageSize).Append(", ").Append(pageSize);
                ds = DBhelper.ExecuteDataset(sbSql.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }

        /// <summary>
        /// function to bind all users
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns>data</returns>
        public static DataTable BindDefaultUsers()
        {
            DataTable dt;
            try
            {

                string sbSql = "SELECT mg.id NodeId, mg.text MemberName, mg.CreateDate FROM umbraconode mg WHERE mg.nodeObjectType = '39eb0f98-b348-42a1-8662-e7eb18487560' order by mg.text asc";
                dt = DBhelper.ExecuteDataset(sbSql).Tables[0];
            }
            catch (Exception)
            {
                throw;
            }
            return dt;
        }

        /// <summary>
        /// function to add new company setting
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="UserDefaultgroupID"></param>
        /// <returns>New company setting</returns>
        public static int AddDefaultCompany(int UserID,int UserDefaultgroupID)
        {
            int intReturnValue = 0;
            try
            {
                var para = new[] {
                    DBhelper.SetParameter("@UserID", UserID),
                    DBhelper.SetParameter("@UserDefaultgroupID", UserDefaultgroupID),
                };

                string sbSql = "Select * From crm_userdefaultcompany Where UserID = @UserID";
                DataSet ds = DBhelper.ExecuteDataset(sbSql, para);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    intReturnValue = 0;
                }
                else
                {
                    sbSql = "insert into crm_userdefaultcompany(UserID,UserDefaultgroupID) values(@UserID,@UserDefaultgroupID)";
                    DBhelper.ExecuteNonQuery(sbSql, para);
                    intReturnValue = 1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return intReturnValue;
        }

        /// <summary>
        /// function to edit company setting
        /// </summary>
        /// <param name="UsercompanyID"></param>
        /// <param name="UserID"></param>
        /// <param name="UserDefaultgroupID"></param>
        /// <returns>0 or 1</returns>
        public static int EditDefaultCompany(int UsercompanyID, int UserID, int UserDefaultgroupID)
        {
            int intReturnValue = 0;
            try
            {
                var para = new[] {
                    DBhelper.SetParameter("@UsercompanyID", UsercompanyID),
                    DBhelper.SetParameter("@UserID", UserID),
                    DBhelper.SetParameter("@UserDefaultgroupID", UserDefaultgroupID),
                };

                string sbSql = "Select * From crm_userdefaultcompany Where UsercompanyID <> @UsercompanyID And  UserID = @UserID And UserDefaultgroupID=@UserDefaultgroupID";
                DataSet ds = DBhelper.ExecuteDataset(sbSql, para);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    intReturnValue = 0;
                }
                else
                {
                    sbSql = "update crm_userdefaultcompany set UserID =@UserID,UserDefaultgroupID=@UserDefaultgroupID where UsercompanyID = @UsercompanyID";
                    DBhelper.ExecuteNonQuery(sbSql, para);
                    intReturnValue = 1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return intReturnValue;
        }



        /// <summary>
        /// function to delete company setting
        /// </summary>
        /// <param name="UsercompanyID"></param>   
        /// <returns>true/false</returns>
        public static bool DeleteDefaultCompany(int UsercompanyID)
        {
            bool flag = false;
            try
            {
                var para = new[] {
                    DBhelper.SetParameter("@UsercompanyID", UsercompanyID),
                };

                string sbSql = "delete from crm_userdefaultcompany where UsercompanyID=@UsercompanyID";
                DBhelper.ExecuteNonQuery(sbSql, para);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
    }
}